// pages/pingjia_order/pingjia_order.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
var regStrFn = common.regStrFn;
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  score: 5,//评分 分数
	  imgarr: [],//存放图片
	  imgArrPath:[],//图片数组（从后台获取）
	  content:'',//评价内容
	},
	//输入 评价内容
	inputcontent: function (e) {
		let val = e.detail.value;
		var pos = e.detail.cursor;
		let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g
		if (!reg.test(val)) {
			this.setData({
				content: val
			})
		} else {
			let obj = regStrFn(val);
			if (pos != -1 && obj.index) {
				// /　　/计算光标的位置
				pos = obj.index.index
			}
			this.setData({
				content: obj.val,
				cursor1: pos
			})
		}
	},
  //设置评分
	set_score:function(e){
		var score = e.currentTarget.dataset.score;//评分
		this.setData({
			score: score
		})
	},
	//添加图片
	addimage: function (e) {
		var that = this;
		var imgarr = this.data.imgarr;//图片数组
		var imgArrPath = this.data.imgArrPath ? this.data.imgArrPath:[];//返回的图片路径
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths[0];
				imgarr.push(tempFilePaths)
				that.setData({
					imgarr: imgarr
				})
				
				//请求图片上传接口
				wx.uploadFile({
					url: url + '/Common/uploadFile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						file: imgarr,
						APP_ID: APP_ID,
						sign: md5([
							"APP_ID" + APP_ID,
						])
					},
					success: function (res) {
						var data = JSON.parse(res.data);
						imgArrPath.push(data.data.src)
						that.setData({
							imgArrPath: imgArrPath,
						})
					}
				})
			}
		})
	},
	//更换图片
	changeimg: function (e) {
		var that = this;
		var imgarr = this.data.imgarr;//图片数组
		var imgArrPath = this.data.imgArrPath ? this.data.imgArrPath : [];//返回的图片路径
		var index = e.currentTarget.dataset.index;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths[0];
				imgarr[index] = tempFilePaths;
				that.setData({
					imgarr: imgarr
				})
				
				//请求图片上传接口
				wx.uploadFile({
					url: url + '/Common/uploadFile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						file: imgarr,
						APP_ID: APP_ID,
						sign: md5([
							"APP_ID" + APP_ID,
						])
					},
					success: function (res) {
						var data = JSON.parse(res.data);
						// imgArrPath.push(data.data.src)
						imgArrPath[index] = data.data.src
						that.setData({
							imgArrPath: imgArrPath,
						})
					}
				})
				
			}
		})
	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this;
	  var order_id = options.id;//订单id
	//   var good_id = options.goodid;//订单id============================
	  var good_id = options.goodid ? options.goodid : 1;//商品id
	  this.setData({
		  order_id: order_id,
		  good_id: good_id
	  })
	  var session_str = wx.getStorageSync('session_str');//获取密钥
	  if (!session_str) {
		  wx.navigateTo({
			  url: '../author/author',
		  })
		  return false;
	  }
	  wx.showLoading({
		  title: '加载中',
		  mask: true,
	  })
	  //   请求订单信息接口
	  wx.request({
		  url: url + 'Order/getOrder',
		  data: {
			  goods_id: good_id,
			  session_str: session_str,
			  APP_ID: APP_ID,
			  sign: md5([
				  "goods_id" + good_id,
				  "session_str" + session_str,
				  "APP_ID" + APP_ID
			  ])
		  },
		  header: {},
		  method: 'post',
		  dataType: 'json',
		  responseType: 'text',
		  success: function (res) {
			  wx.hideLoading();
			  if (res.data.code == 200) {
				  var info_res = res.data.data;
				  that.setData({
					  goodinfo: info_res.goods_info, //商品信息

				  })
			  } else {
				  wx.showToast({
					  title: res.data.msg,
					  icon: 'none',
					  mask: true,
				  })
			  }
		  },
		  fail: function (res) {
			  wx.hideLoading();
			  wx.showToast({
				  title: '网络错误',
				  icon: 'none',
				  mask: true,
			  })
		  }
	  });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  
	  
	  
  },
  //提交评价
	handle_submit:function(e){
		var that = this;
		var content = this.data.content;//评论内容
		var score = this.data.score;//评分 分数
		var imgArrPath = this.data.imgArrPath;//接口返回的图片路径 （再次传给后台）
		var order_id = this.data.order_id ? this.data.order_id:1;//订单id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		if (!content){
			wx.showToast({
				title: '请输入评价内容',
				icon: 'none',
				mask: true,
			})
			return false;
		}
	
		//请求 评价订单的接口
		wx.request({
			url: url + 'Order/commentOrder',
			data: {
				session_str: session_str,
				order_id: order_id,
				body: content,
				score: score,
				imgs: imgArrPath,
				APP_ID: APP_ID,
				sign: md5([
					"session_str" + session_str,
					"order_id" + order_id,
					"body" + content,
					"score" + score,
					"APP_ID" + APP_ID,
				])
			},
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					wx.showToast({
						title: '评论成功',
						icon: 'success',
						mask: true,
					})
					setTimeout(function(){
						wx.navigateBack({
							delta: 1,
						})
					},1500)
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}

			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})